<template>
  <div class="task-preview-container">
    <div class="task-details">
      <div class="task-info">
        <span class="info-label">编程语言:</span>
        <span class="info-value">{{ task.language.name }}</span>
      </div>
      <div class="task-info">
        <span class="info-label">模块名称:</span>
        <span class="info-value">{{ task.module.name }}</span>
      </div>
      <div class="task-info">
        <span class="info-label">难易程度:</span>
        <span class="info-value">{{ task.completed }}</span>
      </div>
      <div class="task-info">
        <el-checkbox value="" name="type" v-model="taskcode.checked">
          不会做
        </el-checkbox>
      </div>
    </div>
    <div class="task-content">{{ task.title }}
      <pre>
        <code></code>
      </pre>
      <!-- 显示答案 -->
      <el-divider content-position="left">您的答案</el-divider>
      <el-input
          v-model="taskcode.code"
          :autosize="{ minRows:15, maxRows: 180 }"
          type="textarea"
          placeholder="请输入任务答案"/>
    </div>
  </div>


</template>

<script>

import HeaderNav from '@/components/HeaderNav.vue'
import TaskContainer from '@/components/TaskContainer.vue'
import taskItem from "@/components/TaskItem.vue";

export default {
  // 获取数据库的编程语言等相关数据
  props: {
    task: {
      type: Object,
      required: true,
    },
  },
  components: {
    HeaderNav,
    TaskContainer,
  },
  data() {
    return {
      taskcode: {
        checked: false,
        code: this.task.answer || "",
      }
    }
  },
  methods: {
    submitCode() {
      const payload = {
        ...this.taskcode,
        // 获取当前任务id
        task_id: this.task.id,
      };
      return this.$api.submit_code(payload).then(
          res => {
            this.$message.success("任务保存成功！");
            return res;
          }).catch(err => {
        console.error("保存失败", err);
        this.$message.error("任务保存失败");
        throw err; // 让父组件可以 .catch
      });
    }
  },
  watch: {
    'task.answer': {
      immediate: true,
      handler(){
        this.taskcode.code = this.task.answer
      }
    }
  },
}
</script>

<style scoped>
.task-preview-container {
  margin: 0 auto;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.task-header {
  text-align: center;
  margin-bottom: 20px;
}

.task-title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
}

.task-details {
  display: flex;
  justify-content: space-between;
  line-height: 40px;
  background-color: #f3f3f3;
  margin-bottom: 10px;
  border-radius: 8px;
}

.task-info {
  flex: 1;
  text-align: center;
}

.info-label {
  font-weight: bold;
  color: #555;
  margin-right: 10px;
}

.info-value {
  color: #777;
}

.task-content {
  background: #fff;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  height: 600px;
  overflow: auto;
}

.task-content pre:has(>code) {
  background: #F0F2F5;
  color: #333;
  padding: 10px;
  border-radius: 5px;
  overflow-x: auto;
}

.task-content code {
  font-family: 'Courier New', Courier, monospace;
  font-size: 14px;
}
</style>